package com.wecheir.utils;

import org.springframework.expression.ParseException;

import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil {

    //日期转换为年月日字符串
    public static String getStringDateShort(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("MM月dd日");
        String dateString = formatter.format(date);
        return dateString;
    }

    //日期转换为年月日字符串
    public static String getStringDate(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(date);
        return dateString;
    }
    //年月日字符串转换为日期
    public static Date getDateString(String dateString)  {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = formatter.parse(dateString);
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
    public static List<String> getBetweenDates(String start, String end) {

        List<String> result = new ArrayList<String>();

        try {

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            Date start_date = sdf.parse(start);

            Date end_date = sdf.parse(end);

            Calendar tempStart = Calendar.getInstance();

            tempStart.setTime(start_date);

            Calendar tempEnd = Calendar.getInstance();

            tempEnd.setTime(end_date);

            while (tempStart.before(tempEnd)||tempStart.equals(tempEnd)) {

                result.add(sdf.format(tempStart.getTime()));

                tempStart.add(Calendar.DAY_OF_YEAR, 1);

            }

        } catch (ParseException | java.text.ParseException e) {

            e.printStackTrace();

        }

        Collections.reverse(result);

        return result;

    }

    /**
     * 获取过去一周的日期列表
     * @param date
     * @return
     */
    public static List<Date> getPastWeek(Date date){
        //设置日历对象
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date);

        List<Date> list = new ArrayList<>(7);
        list.add(date);

        for(int i = 1; i<7; i++){
            calendar.add(Calendar.DATE, -1);
            list.add(calendar.getTime());
        }

        return list;
    }

    public static String[] toStringArr(List<Date> dateList){
        String[] strings = new String[dateList.size()];

        for (int i = 0; i<dateList.size(); i++){
            strings[i] = getStringDateWithColon(dateList.get(i));
        }

        return strings;
    }


    public static String getStringDateWithColon(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("MM:dd");
        String dateString = formatter.format(date);
        return dateString;
    }
}
